<?php

/**
 * @author Justina Lee
 */
class eresources_model extends CI_Model {

    var $name = '';
    var $description = '';
    var $image = '';
    var $categoryName = '';
    var $eResourcesUrls = array();

    function __construct() {
        parent::__construct();
    }
/*
|--------------------------------------------------------------------------
| parseXml (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function calls the parseEResourceXml and retCategorizedList to parse
| the eResources and eResources categories XML. 
|
*/     
    function parseXml($xmlDatabaseUrl, $xmlCategoryUrl) {
        $eResources = $this->parseEResourceXml($xmlDatabaseUrl);
        $output = $this->retCategorizedList($xmlCategoryUrl, $eResources);

        return $output;
    }
    
/*
|--------------------------------------------------------------------------
| parseCategoryXml (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function parses the eResources categories XML.
|
*/ 
    function parseCategoryXml($xmlCategoryUrl){
        $categoryList[] = array('name' => 'All');
        $retList = array();
        $xmlData = simplexml_load_file($xmlCategoryUrl);
        
        //total number of categories
        $categoryCount = count($xmlData->category);
        
        for ($i = 0; $i < $categoryCount; $i++) {
            $category = $xmlData->category[$i];
            
            $categoryList[] = array('name'=>(string)$category->name);
        }
        
        $retList = array('categories'=>$categoryList);
        
        return $retList;
    }
    
/*
|--------------------------------------------------------------------------
| parseEResourceXml (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function parses the eResources XML.
|
*/ 
    function parseEResourceXml($xmlDatabaseUrl) {
        $count = 1;
        $eResources = array();
        $imageFolderUrl = $this->config->item('image_folder');

        $eResourcesData = simplexml_load_file($xmlDatabaseUrl);

        //total number of eResources
        $eResourcesCount = count($eResourcesData->array->dict);
        //for each of the resources
        for ($i = 0; $i < $eResourcesCount; $i++) {
            $eResource = $eResourcesData->array->dict[$i];
            $item = new eresources_model();
            $item->name = $eResource->string[0];
            $item->description = $eResource->string[1];
            $item->image = $imageFolderUrl . $count . '.png';

            //total number of urls
            $urlsCount = count($eResource->array->string);
            //for each of the urls
            for ($k = 0; $k < $urlsCount; $k++) {
                $url = $eResource->array->string[$k];
                $item->eResourcesUrls[] = $url;
            }
            $count++;
            $eResources[(string) $item->name] = $item;
        }

        return $eResources;
    }
    
/*
|--------------------------------------------------------------------------
| retCategorizedList (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function parses the eResources categories XML and categorises the 
| databases and returns the final output for the eResources module.
|
*/ 
    function retCategorizedList($xmlCategoryUrl, $eResources) {
        $retList = array();
        $eResourceList = array();

        $xmlData = simplexml_load_file($xmlCategoryUrl);

        //total number of categories
        $categoryCount = count($xmlData->category);

        //for each of the category
        for ($i = 0; $i < $categoryCount; $i++) {
            $category = $xmlData->category[$i];
            
            //total number of eResources
            $eResourceCount = count($category->eResources->eResource);
            //for each of the eResource
            for ($k = 0; $k < $eResourceCount; $k++) {
                $eResource = $category->eResources->eResource[$k];

                //get item from eResources
                $item = $eResources[(string) $eResource];
                $item->categoryName = $category->name;

                //total number of urls
                $urlCount = count($item->eResourcesUrls);
                //initialize list
                $urlList = array();
                //for each of the urls
                for ($j = 0; $j < $urlCount; $j++) {
                    $urlList[] = array('url' => (string) $item->eResourcesUrls[$j]);
                }

                $eResourceList[] = array('categoryName' => (string) $item->categoryName,
                    'name' => (string) $item->name,
                    'description' => (string) $item->description,
                    'image' => (string) $item->image, 'eResourcesUrls' => $urlList);
            }
        }
        $retList = array('eResources' => $eResourceList);
        return $retList;
    }
}

?>   